package cn.aj.system.controller;

import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import cn.aj.system.domain.Type;
import cn.aj.system.query.TypeQuery;
import cn.aj.system.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/typed")
@Api(value = "字典类型的api", description = "字典类型的crud以及其它功能")
public class TypeController {

    @Autowired
    private TypeService typeService;
    
    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:18
     * 类型查询全部
    */
    @GetMapping
    @ApiOperation(value = "字典类型查询全部")
    public List<Type> loadAll() {
        return typeService.loadAll();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:19
     * 类型分页查询以及高级查询
    */
    @PostMapping
    @ApiOperation(value = "字典类型分页查询")
    public PageListUtil<Type> loadPage(@ApiParam(value="字典类型封装的每页条数以及页码",required = true) @RequestBody TypeQuery typeQuery) {
        PageListUtil<Type> list = typeService.loadPage(typeQuery);
        return list;
    }

   /**
   * @Description: TODO
   * @Author: aj
   * @Version: 1.0
   * @Date:  2022/11/18 0018 10:19
    * 类型根据id查询
   */
    @GetMapping("/{id}")
    @ApiOperation(value = "字典类型根据id查询")
    public Type getById(@ApiParam(value="字典类型id",required = true) @PathVariable("id") Long id) {
        return typeService.getById(id);
    }


    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:19
     * 类型根据id删除
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "字典类型删除")
    public AjaxResultUtil delete(@ApiParam(value="字典类型字典id",required = true) @PathVariable("id") Long id) {
        typeService.delete(id);
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:19
     * 类型新增或者修改
    */
    @PutMapping
    @ApiOperation(value = "字典类型新增或者修改")
    public AjaxResultUtil addOrUpdate(@ApiParam(value="字典类型实体类",required = true) @RequestBody Type type) {
        if (type.getId() == null) { // 新增
            typeService.add(type);
        } else { // 修改
            typeService.update(type);
        }
        return AjaxResultUtil.getAjaxResult().success();
    }

   /**
   * @Description: TODO
   * @Author: aj
   * @Version: 1.0
   * @Date:  2022/11/18 0018 10:20
    * 类型批量删除
   */
    @PatchMapping
    @ApiOperation(value = "字典类型批量删除")
    public AjaxResultUtil batchDelete(@ApiParam(value="字典类型id",required = true) @RequestBody List<Long> ids) {
        typeService.batchDelete(ids);
        return AjaxResultUtil.getAjaxResult().success();
    }
}
